package com.sevenstudio.adu.gasing.state;

import org.andengine.engine.Engine.EngineLock;
import org.andengine.entity.scene.ITouchArea;
import org.andengine.input.touch.TouchEvent;

import android.view.KeyEvent;

import com.sevenstudio.adu.gasing.bluetooth.BT_Server_Client;
import com.sevenstudio.adu.gasing.define.GameEngineConfiguration;
import com.sevenstudio.adu.gasing.define.iGameDefines;
import com.sevenstudio.adu.gasing.define.iServer_Data;
import com.sevenstudio.adu.gasing.element.AScene;
import com.sevenstudio.adu.gasing.element.ASound;
import com.sevenstudio.adu.gasing.element.ASprite;
import com.sevenstudio.adu.gasing.engine.GameEngine;
import com.sevenstudio.adu.gasing.engine.Utils;

public class State_Game_Win implements iGameDefines
{
	ASprite		bgMenu, btnContinue, btnMenu, icon, ekspresi, text, score, btnNext, txt_unlock;
	AScene		scene;
	GameEngine	engine;
	ASound		sound;
	public static boolean isRestart, isMenu;

	public State_Game_Win(GameEngine engine)
	{
		this.engine = engine;
		
		bgMenu = new ASprite(engine, SPRITE_GAME, SPRITE_GAME_MENU_BG);
		btnMenu = new ASprite(engine, SPRITE_GAME, SPRITE_GAME_BTN_MENU);
		ekspresi = new ASprite(engine, SPRITE_GAME, SPRITE_GAME_MENU_EKSPRESI_MENANG);
		text = new ASprite(engine, SPRITE_GAME, SPRITE_GAME_MENU_TEXT_MENANG);
		score = new ASprite(engine, SPRITE_GAME, SPRITE_GAME_MENU_SCORE_MENANG);
		sound = new ASound(engine, SFX_GAME, SFX_GAME_MENANG);
		
		txt_unlock = new ASprite(engine, SPR_START_CARIEER, 4);
	}
	
	public void Init()
	{		
		isRestart = isMenu = false;
		sound.Play();


		if(BT_Server_Client.isBluetooth)
			btnContinue = new ASprite(engine, SPRITE_GAME, SPRITE_GAME_BTN_CONTINUE);
		else
		{
			btnContinue = new ASprite(engine, SPRITE_GAME, SPRITE_GAME_BTN_RESTART);			
			btnNext = new ASprite(engine, SPRITE_GAME, SPRITE_GAME_BTN_CONTINUE);
		}
		

		scene = engine.getGameScene();

		if(BT_Server_Client.isBluetooth && !BT_Server_Client.isServer)
		{
			icon = new ASprite(engine, SPRITE_GAME, SPRITE_GAME_MENU_ACEH + iServer_Data.INDEX_RIVAL);
		}
		else if(GameEngineConfiguration.ModeSelected == MODE_CAREER)
		{
			icon = new ASprite(engine, SPRITE_MC_CAREER, DEFAULT_GASING_PAUSE);
		}
		else
		{
			icon = new ASprite(engine, SPRITE_GAME, SPRITE_GAME_MENU_ACEH + iServer_Data.INDEX_MC);
		}


		btnMenu.SetPos(SPRITE_GAME[SPRITE_GAME_BTN_MENU].getX(),
				SPRITE_GAME[SPRITE_GAME_BTN_MENU].getY() + 20,
				SPRITE_GAME[SPRITE_GAME_BTN_MENU].getAnchor());
		
		
		
//		engine.SetGameScene(scene);
		Utils.TRACE("WWWWW");
		
		engine.SceneAttach(scene, bgMenu);
		engine.SceneAttach(scene, btnContinue);
		
		
		engine.SceneAttach(scene, btnMenu);
		engine.SceneAttach(scene, icon);
		engine.SceneAttach(scene, ekspresi);
		engine.SceneAttach(scene, text);
		engine.SceneAttach(scene, score);
		
		scene.theScene().registerTouchArea(btnContinue.theSprite());
		
		if(GameEngineConfiguration.ModeSelected == iGameDefines.MODE_CAREER)
		{
		score.theSprite().setY(score.GetY() - Utils.GetRatioHeight(30));
		btnContinue.theSprite().setY(btnContinue.GetY() - Utils.GetRatioHeight(20));
		engine.SceneAttach(scene, btnNext);
		btnNext.theSprite().setY(btnNext.GetY() + Utils.GetRatioHeight(40));
		scene.theScene().registerTouchArea(btnNext.theSprite());
		
		btnMenu.theSprite().setY(btnMenu.GetY() + Utils.GetRatioHeight(35));
		}
		
		scene.theScene().registerTouchArea(btnMenu.theSprite());
		scene.theScene().setOnAreaTouchListener(engine);
		
		iServer_Data.poin +=100;
		
		if(GameEngineConfiguration.ModeSelected == iGameDefines.MODE_CAREER)
		{
			if(!iServer_Data.isUnlockGasing[GameEngineConfiguration.Level_Career])
			{
				iServer_Data.isUnlockGasing[GameEngineConfiguration.Level_Career] = true;
				
				engine.SceneAttach(scene, txt_unlock);
			}
		
		}
	}
	
	public void Update()
	{	
		if(isMenu)
		{
			isMenu = false;
			menu();

//			if(BT_Server_Client.isBluetooth)
//			BT_Server_Client.stopBTThread();
		}	
		if(isRestart)
		{
			isRestart = false;
			restart();
		}
	}
	
	public void Touch(TouchEvent pSceneTouchEvent, ITouchArea pTouchArea,
			float pTouchAreaLocalX, float pTouchAreaLocalY)
	{
		if (pSceneTouchEvent.isActionUp() && pTouchArea.equals(btnContinue.theSprite()))
		{

			if(BT_Server_Client.isBluetooth)
			{
				restart();
				engine.server_Client.sendMessage("restart");
			}
			else
			{
				final EngineLock engineLock = engine.getEngine().getEngineLock();
				engineLock.lock();
				
				scene.theScene().detachChildren();
	
				State_Game_Play.reset();
				scene.theScene().detachChildren();		
				engineLock.unlock();
				
				engine.SetNextMessageState(0);
				engine.SetNextGameState(GAME_STATE_GAMEPLAY);
			}
			
		}
		else if (pSceneTouchEvent.isActionUp() && pTouchArea.equals(btnMenu.theSprite()))
		{
			menu();
			if(BT_Server_Client.isBluetooth)
			{
				engine.server_Client.sendMessage("menu");
			}
		}
		
		else if (pSceneTouchEvent.isActionUp() && pTouchArea.equals(btnNext.theSprite()) 
				&& GameEngineConfiguration.ModeSelected == iGameDefines.MODE_CAREER)
		{
			GameEngineConfiguration.Level_Career++;
						
//			final EngineLock engineLock = engine.getEngine().getEngineLock();
//			engineLock.lock();

			if(BT_Server_Client.isBluetooth)
			{
				restart();
				engine.server_Client.sendMessage("restart");
			}
			else
			{
				final EngineLock engineLock = engine.getEngine().getEngineLock();
				engineLock.lock();
				
				State_Game_Play.reset();
				scene.theScene().detachChildren();		
				State_Game_Play.exit();
				engineLock.unlock();
								
				engine.SetNextMessageState(0);
				engine.SetNextGameState(GAME_STATE_GAMEPLAY);
				
				
			}


		}


		else if (pSceneTouchEvent.isActionUp() && pTouchArea.equals(btnMenu.theSprite()))
		{
			menu();
			if(BT_Server_Client.isBluetooth)
			{
				engine.server_Client.sendMessage("menu");
			}
		}

		
	}
	
	public void keyBack(int keyCode, KeyEvent event)
	{
		if (keyCode == KeyEvent.KEYCODE_BACK)
		{
			menu();
			if(BT_Server_Client.isBluetooth)
			{
				engine.server_Client.sendMessage("menu");
			}
		}
		
	}
	
	private void menu()
	{
		final EngineLock engineLock = engine.getEngine().getEngineLock();
		engineLock.lock();

		State_Game_Play.reset();
		scene.theScene().detachChildren();
		State_Game_Play.exit();
		
		engineLock.unlock();

		if(BT_Server_Client.isBluetooth)
		engine.getEngine().disableAccelerationSensor(engine);
		engine.SetNextMessageState(0);
		engine.SetNextGameState(GAME_STATE_MAINMENU);
	}
	
	private void restart()
	{
		final EngineLock engineLock = engine.getEngine().getEngineLock();
		engineLock.lock();

		State_Game_Play.reset();
		scene.theScene().detachChildren();
		State_Game_Play.exit();
		
		engineLock.unlock();
		engine.SetNextMessageState(0);
		engine.SetNextGameState(GAME_STATE_GAMEPLAY);
	}
}
